package cn.net.greenet.policy.servlet;

import cn.net.greenet.common.policy_service.PolicySupport;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambimmort.prserver.bean.RepositoryOperationLogBean;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

/**
 * 根据MessageType、MessageNo和PolicyInstance 来查看策略的历史信息
 *
 * @author liangdelin
 */
@WebServlet(name = "HistoryPolicyInfo", urlPatterns = {"/policy/history_policy_info"})
public class HistoryPolicyInfo extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String type = request.getParameter("type"); //创建策略仓库类型名称
        byte[] instanceArr = Base64.decodeBase64(request.getParameter("instance"));
        String instance = new String(instanceArr, "utf-8");//创建策略仓库实例名称
        String messageNo = request.getParameter("messageNo"); //策略的MessageNo 号

        List<RepositoryOperationLogBean> beans = PolicySupport.getInstance().getApi().getMessageLogs(type, instance, Integer.parseInt(messageNo));
        Map<Long, String> strMap;  //通过ID来进行排序 也就是这个MessageNo的版本号  默认的是按照升序排列
        strMap = new TreeMap<Long, String>();

        for (RepositoryOperationLogBean bean : beans) {
            JSONObject obj = new JSONObject();
            obj.put("MessageNo", bean.getSvnFile().getMessageNo() + "");
            obj.put("MessageType", type);
            obj.put("Instance", instance);
            obj.put("MessageSerialNo", bean.getId());  //策略的版本号
            obj.put("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(bean.getCreateTime())));
            JSON json = JSONObject.fromObject(bean.getSvnFile().getContent());
            obj.put("Content", json);
            obj.put("Comment", bean.getSvnFile().getComment());
            strMap.put(bean.getId(), obj.toString());
        }

        JSONArray jor = new JSONArray();
        for (long key : strMap.keySet()) {
            jor.add(strMap.get(key));
        }

        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println(jor.toString());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
